package com.gxa.modules.pc.hospital.controller;

import com.gxa.common.exception.ResultException;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.model.entity.hospital.*;
import com.gxa.modules.oss.factory.OSSFactory;
import com.gxa.modules.pc.hospital.service.HospitalParkService;
import com.gxa.modules.pc.hospital.service.ParkGuidanceService;
import com.gxa.modules.pc.hospital.service.ProvinceService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 院区表 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:04:59
 */
@Api(tags = "医院导航")
@RestController
@RequestMapping("/pc/park")
public class HospitalParkController {
    //时间转换器
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

//    院区
    @Autowired
    private HospitalParkService hospitalParkService;

    //院区导航
    @Autowired
    private ParkGuidanceService parkGuidanceService;

    //地区
    @Autowired
    private ProvinceService provinceService;

    @ApiOperation(value = "院区查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "totalCount",value ="总记录数",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "pageSize",value ="每页显示多少条",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "name",value ="查询条件（园区）",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "totalPage",value ="总页数",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "currPage",value ="当前是第几页",dataType ="int"),

    })
    @ApiResponse(code = 0, message = "success", response = HospitalPark.class)
    @GetMapping
    public Result<PageUtils> queryByPage (@RequestParam @ApiIgnore Map<String,Object> params){
        PageUtils pageUtils = this.hospitalParkService.queryByName(params);
        return new Result<PageUtils>().ok(pageUtils);
    }


    @ApiOperation(value = "院区删除")
    @ApiImplicitParam(paramType = "body",name = "ids",value ="要删除数据的id集合",dataType ="List")
    @DeleteMapping
    public Result delById (@RequestBody List<Long> ids){

        String s = hospitalParkService.delByIds(ids);

        return new Result().ok(s);
    }


    @ApiOperation(value = "院区新增")
    @PostMapping
    public Result add(@RequestBody HospitalPark hospitalPark){

        hospitalParkService.add(hospitalPark);

        return new Result().ok();
    }

    @ApiOperation(value = "院区修改")
    @PutMapping
    public Result update(@RequestBody HospitalPark hospitalPark){

        hospitalParkService.update(hospitalPark);

        return new Result().ok();
    }

    @ApiOperation(value = "根据院区的id查询院区信息")
    @ApiImplicitParam(paramType = "query",name = "id",value ="园区的id",dataType ="Integer")
    @GetMapping("/query")
    public Result<HospitalPark> queryById(Integer id){
        HospitalPark hospitalPark = hospitalParkService.queryById(id);
        return new Result<HospitalPark>().ok(hospitalPark);
    }

    @ApiOperation(value = "院内导航查询", response = ParkGuidance.class)
    @ApiImplicitParam(paramType = "query",name = "id",value ="园区的id",dataType ="Integer")
    @GetMapping("/guidance")
    public Result queryByIdToParkGuidance(Integer id){
        List<ParkGuidance> parkGuidances = parkGuidanceService.queryByIdToParkGuidance(id);
        return new Result().ok(parkGuidances);
    }

    @ApiOperation(value = "院内导航增加")
    @PostMapping("/guidance")
    public Result addParkGuidance(ParkGuidance parkGuidance){
        parkGuidanceService.add(parkGuidance);
        return new Result().ok();
    }

    @ApiOperation(value = "院内导航删除")
    @ApiImplicitParam(paramType = "query",name = "parkGuidanceId",value ="院内导航的id",dataType ="Integer")
    @DeleteMapping("/guidance/{parkGuidanceId}")
    public Result delParkGuidance(Integer parkGuidanceId){
        parkGuidanceService.del(parkGuidanceId);
        return new Result().ok();
    }

    @ApiOperation(value = "省份", response = Province.class)
    @GetMapping("/getProvinces")
    public Result getProvinces() {
        List<Province> provinces = provinceService.getProvinces();
        return new Result().ok(provinces);
    }

    @ApiOperation(value = "区", response = City.class)
    @GetMapping(value = "/getCity")
    public Result getCity(@RequestParam String province) {
        List<City> city = provinceService.getCity(province);
        return new Result().ok(city);
    }

    @ApiOperation(value = "县", response = Area.class)
    @GetMapping(value = "/getArea")
    public Result getArea(@RequestParam String city) {

        List<Area> area = provinceService.getArea(city);

        return new Result().ok(area);
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("/up")
    public Result<String> upload(@RequestParam("file") MultipartFile file)throws Exception {
        if (file.isEmpty()) {
            throw new ResultException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return new Result<String>().ok(url);
    }

}
